package com.ly.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ly.bean.*;
import com.ly.mapper.EasybuyProductCategoryMapper;
import com.ly.mapper.EasybuyProductCommentMapper;
import com.ly.mapper.EasybuyProductImagesMapper;
import com.ly.mapper.EasybuyProductMapper;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sun.deploy.cache.Cache.exists;

/**
 * @author 李岩
 * @date 2022/1/7-10:56
 * @describe
 */
@Configuration
@EnableScheduling
public class ProductPageTaskServiceImpl {

    @Autowired
    private EasybuyProductMapper productMapper;
    @Autowired
    private EasybuyProductCategoryMapper productCategoryMapper;
    @Autowired
    private EasybuyProductImagesMapper productImagesMapper;
    @Autowired
    private EasybuyProductCommentMapper productCommentMapper;
    @Autowired
    private FreeMarkerConfig freeMarkerConfig;

    @Scheduled(cron = "05 22 11 * * ?")
    public void createProductPage(){


        //一级商品分类
        List<EasybuyProductCategory> firstList=null;
        //一级商品分类下面的二级商品分类
        HashMap<Integer, List<EasybuyProductCategory>> secondMap = new HashMap<>();
        //二级商品分类下面的三级商品分类
        HashMap<Integer, List<EasybuyProductCategory>> thirdMap = new HashMap<>();
        //一级商品分类下面的所有三级商品分类
        HashMap<Integer, List<EasybuyProductCategory>> thirdAllCatetoryMap = new HashMap<>();
        //一级商品分类下面的前六件商品
        HashMap<Integer, List<EasybuyProduct>> firstProductMap = new HashMap<>();
        try {
            //获取所有的商品信息
            List<EasybuyProduct> productList = productMapper.selectAllProduct();
            for (EasybuyProduct product : productList) {
                System.out.println("生成："+product.getName()+"的页面!");
                //获取每一个页面的明细数据
                //生成页面
                File file = new File("D:\\Idea\\workspace\\dubbo_springboot\\easybuy_springboot_dubbo\\easybuy_shop_web\\src\\main\\resources\\static\\product\\ProductDetail_" + product.getId() + ".html");
                if (file.exists()){
                    file.delete();
                }

                //一级商品下面的分类
                firstList = productCategoryMapper.findCategoryByPid(0);
                for (EasybuyProductCategory firstCategory : firstList) {
                    //一级商品分类下面的所有三级商品分类
                    List<EasybuyProductCategory> firstAllCatetoryList = productCategoryMapper.findThirdCategoryByPid(firstCategory.getId());
                    thirdAllCatetoryMap.put(firstCategory.getId(), firstAllCatetoryList);

                    //一级商品分类下面的二级商品分类
                    List<EasybuyProductCategory> secondList = productCategoryMapper.findCategoryByPid(firstCategory.getId());
                    secondMap.put(firstCategory.getId(), secondList);
                    //一级商品分类下面的前六件商品
                    List<EasybuyProduct> firstProductList = productMapper.selectSixByCategoryLevel1Id(firstCategory.getId());
                    firstProductMap.put(firstCategory.getId(), firstProductList);
                    for (EasybuyProductCategory secondCategory : secondList) {
                        //二级商品分类下面的三级商品分类
                        List<EasybuyProductCategory> thirdList = productCategoryMapper.findCategoryByPid(secondCategory.getId());
                        thirdMap.put(secondCategory.getId(), thirdList);
                    }
                }

                //根据商品id展示商品信息
                //EasybuyProduct product = productMapper.selectProductByPrimaryKey(productId);
                //根据商品id查询商品的附属图片数据
                List<EasybuyProductImages> productImagesList = productImagesMapper.selectProductImagesByPid(product.getId());
                //根据商品id查询该商品的评论信息
                PageInfo<EasybuyProductComment> commentPageInfo = findCommentByPageInfo(product.getId(),1);
                //根据商品id统计该商品的评论占比
                Map<String, Object> commentMap = productCommentMapper.selectLevelCommentNUmBypid(product.getId());
                //该件商品的总评论数
                Integer zts = Integer.parseInt(commentMap.get("zts").toString());
                //好评的个数
                Integer hps = Integer.parseInt(commentMap.get("hps").toString());
                //中评的个数
                Integer zps = Integer.parseInt(commentMap.get("zps").toString());
                //差评的个数
                Integer cps = Integer.parseInt(commentMap.get("cps").toString());
                //计算百分比
                int hpsbfb = (int) ((hps * 1.0 / zts) * 100);
                int zpsbfb = (int) ((zps * 1.0 / zts) * 100);
                int cpsbfb = (int) ((cps * 1.0 / zts) * 100);
                Map<String, Object> resuMap = new HashMap<>();

                resuMap.put("product", product);
                resuMap.put("productImagesList", productImagesList);
                resuMap.put("commentList", commentPageInfo.getList());
                resuMap.put("pages", commentPageInfo.getPages());
                resuMap.put("hpsbfb", hpsbfb);
                resuMap.put("zpsbfb", zpsbfb);
                resuMap.put("cpsbfb", cpsbfb);
                //System.out.println("hpsbfb="+hpsbfb+";zpsbfb="+zpsbfb+";cpsbfb="+cpsbfb);
                System.out.println("secondMap=" + secondMap);
                System.out.println("thirdMap=" + thirdMap);
                resuMap.put("firstList", firstList);
                resuMap.put("secondMap", secondMap);
                resuMap.put("thirdMap", thirdMap);
                resuMap.put("thirdAllCatetoryMap", thirdAllCatetoryMap);
                resuMap.put("firstProductMap", firstProductMap);

                //生成该页面
                freemarker.template.Configuration configuration = freeMarkerConfig.getConfiguration();
                Template template = configuration.getTemplate("ProductDetail.ftl");
                FileWriter writer = new FileWriter(new File("D:\\Idea\\workspace\\dubbo_springboot\\easybuy_springboot_dubbo\\easybuy_shop_web\\src\\main\\resources\\static\\product\\ProductDetail_" + product.getId() + ".html"));
                template.process(resuMap, writer);
                writer.flush();
                writer.close();
                Thread.sleep(10000);
            }
        }catch (Exception e){
            e.printStackTrace();
        }


    }


    public PageInfo<EasybuyProductComment> findCommentByPageInfo(Integer pid,Integer pageNum){

        try {
            PageHelper.startPage(pageNum,3);
            List<EasybuyProductComment> list = productCommentMapper.selectProductCommentsKey(pid);
            PageInfo<EasybuyProductComment> pageInfo = new PageInfo<>(list);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
